package com.mgq.juc.cas;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author MaGuangQi
 * @description
 * @date 2021-12-23 15:35
 **/
@Slf4j
public class TestCountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        //使用线程池来改进. 我们让线程池内的3个线程参与任务,第4个线程等待前3个任务运行结束
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.execute(() -> {
            try {
                log.debug("线程一开始");
                Thread.sleep(1000);
                latch.countDown();
                log.debug("线程一结束,{}", latch.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.execute(() -> {
            try {
                log.debug("线程二开始");
                Thread.sleep(1500);
                latch.countDown();
                log.debug("线程二结束,{}", latch.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.execute(() -> {
            try {
                log.debug("线程三开始");
                Thread.sleep(5000);
                latch.countDown();
                log.debug("线程三结束,{}", latch.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        log.debug("主线程等待");
        latch.await();
        log.debug("主线程结束等待");
        executorService.shutdown();
        /*executorService.execute(() -> {
            try {
                log.debug("等待");
                latch.await();
                log.debug("结束等待");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });*/
    }

    private static void test1() {
        CountDownLatch latch = new CountDownLatch(3);
        new Thread(() -> {
            try {
                log.debug("开始");
                Thread.sleep(1000);
                latch.countDown();
                log.debug("结束,{}", latch.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1").start();
        new Thread(() -> {
            try {
                log.debug("开始");
                Thread.sleep(1500);
                latch.countDown();
                log.debug("结束,{}", latch.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2").start();
        new Thread(() -> {
            try {
                log.debug("开始");
                Thread.sleep(2000);
                latch.countDown();
                log.debug("结束,{}", latch.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3").start();
        try {
            log.debug("开始等待");
            latch.await();
            log.debug("结束等待");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
